package cn.bossfriday.im.common.poc.model.enums;

import java.util.Arrays;

/**
 * ConnectType
 *
 * @author chenx
 */
public enum ConnectType {

    /**
     * TCP
     */
    TCP(1, "TCP"),

    /**
     * TCP-over-TLS
     */
    TCP_OVER_TLS(2, "TCP-over-TLS");

    private final String name;
    private final int code;

    ConnectType(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public int getCode() {
        return this.code;
    }

    /**
     * getByCode
     *
     * @param code
     * @return
     */
    public static ConnectType getByCode(int code) {
        for (ConnectType entry : ConnectType.values()) {
            if (entry.code == code) {
                return entry;
            }
        }

        return null;
    }

    /**
     * getByName
     *
     * @param name
     * @return
     */
    public static ConnectType getByName(String name) {
        for (ConnectType entry : ConnectType.values()) {
            if (entry.name.equalsIgnoreCase(name)) {
                return entry;
            }
        }

        return null;
    }

    /**
     * getNames
     *
     * @return
     */
    public static String[] getNames() {
        return Arrays.stream(ConnectType.values())
                .map(ConnectType::getName)
                .toArray(String[]::new);
    }
}
